<!-- 我的报告列表 -->
<template>
	<publicPage>
		<view class="orderListPage">
			<scroll-view
				v-if="list.length && listLoad"
				scroll-y
				style="height: 100%"
				@scrolltolower="scrolltolower"
				refresher-enabled
				:refresher-triggered="refresherTriggered"
				@refresherrefresh="refresherrefresh"
			>
				<view v-for="(item, index) in list" :key="index" class="bgf forBox">
					<view class="pt-15" style="background-color: #f5f5f5"></view>
					<view>
						<view class="disFlxJS px-15 borderB linh-50">
							<view>{{ item.itemName }}</view>
							<view :style="'color: ' + reportStatusFind(item.reportStatus, 'colors')">{{
								reportStatusFind(item.reportStatus, "label")
							}}</view>
						</view>
						<view class="disFlxJS linh-26 px-15 pt-10">
							<view class="color8 shrink0 pr-50">服务机构</view>
							<view>{{ item.checkAgencyName }}</view>
						</view>
						<view class="disFlxJS linh-26 px-15">
							<view class="color8 shrink0 pr-50">消费人信息</view>
							<view>{{ item.checkUser }} {{ item.userPhone || "" }}</view>
						</view>
						<view class="disFlxJS linh-26 px-15">
							<view class="color8 shrink0 pr-50">检测时间</view>
							<view>{{ item.reportCheckTime || '-' }}</view>
						</view>
						<view class="disFlxJS linh-26 px-15 pb-10">
							<view class="color8 shrink0 pr-50">报告出具时间</view>
							<view>{{ item.reportTime || '-' }}</view>
						</view>
					</view>

					<view v-if="item.reportStatus === '2'" class="disFlxAC flexEnd pa-15 borderT">
						<view class="btnBox" style="width: 165rpx">
							<u-button
								type="primary"
								shape="circle"
								:customStyle="{ height: '52rpx', fontSize: '28rpx' }"
								@click="downloadConfirm(item)"
								>查看报告</u-button
							>
						</view>
					</view>
				</view>
			</scroll-view>
			<view
				style="height: calc(100% - 50px)"
				class="defaultPage disFlxCC"
				v-if="!list.length && listLoad"
			>
				<view class="fz-18 color8">暂无检验检查报告</view>
			</view>
		</view>
	</publicPage>
</template>

<script>
import orderApi from "@/api/order.js";

export default {
	data() {
		return {
			reportStatus: [
				{ label: "待出报告", value: "0", colors: "#FF5F5F" }, // 待专家付款 显示关闭订单按钮
				{ label: "报告生成中", value: "1", colors: "#BB9313" }, // 待用户付款 显示关闭订单按钮
				{ label: "已出报告", value: "2", colors: "#0D8BFF" }, // 待用户付款 显示关闭订单按钮
			],

			list: [],
			listLoad: false, // 加载是否完成
			getParams: {
				pageNum: 1,
				pageSize: 10,
				total: 0,
			},
			refresherTriggered: false,
		};
	},
	onLoad() {
		this.getList();
	},
	methods: {
		// 获取订单列表
		async getList() {
			try {
				const { data, total } = await orderApi.getReportList(this.getParams);
				data.forEach((v) => {
					v.reportTime = v.reportTime ? v.reportTime.substr(0, v.reportTime.length - 3) : "";
				});
				this.list = this.getParams.pageNum === 1 ? data : [...this.list, ...data];
				this.getParams.total = total;
				this.listLoad = true;
			} catch (error) {
				this.listLoad = true;
			}
		},
		// 加载更多
		scrolltolower() {
			if (this.list.length !== this.getParams.total) {
				// u-view内部节流函数
				uni.$u.throttle(() => {
					this.getParams.pageNum++;
					this.getList();
					console.log(this.getParams.pageNum, "滚动加载");
				}, 2000);
			} else {
				console.log("没有更多了");
			}
		},
		async refresherrefresh() {
			this.refresherTriggered = true;
			this.$data.getParams.pageNum = this.$options.data().getParams.pageNum;
			this.$data.getParams.total = this.$options.data().getParams.total;
			await this.getList();
			console.log(this.getParams.pageNum, "下拉刷新");
			setTimeout(() => {
				this.refresherTriggered = false;
			}, 1000);
		},
		// 获取订单状态
		reportStatusFind(val, keys) {
			const obj = this.reportStatus.find((v) => v.value === val);
			if (!obj) {
				console.log(obj, "后端订单状态异常对象");
			}
			return obj[keys];
		},
    // 查看报告
		downloadConfirm(item) {
			const newPath = `${wx.env.USER_DATA_PATH}/${item.itemName}.pdf`;
      console.log(newPath,11111);
			uni.downloadFile({
				url: item.reportUrl,
				filePath: newPath,
				success: function (res) {
					uni.openDocument({
						filePath: newPath,
						showMenu: true,
						fileType: "pdf",
						success: function (res) {},
					});
				},
				fail: function (res) {
					uni.hideLoading();
				},
			});
		},
	},

	created() {},

	computed: {},
};
</script>
<style lang="scss" scoped>
.orderListPage {
	height: 100%;
}
</style>
